/*
Copyright 2009 - 2013 Peter Krajnc, SCREENWORKER

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.compactmvc.services.flex.remoteobjectservice
{
	import org.compactmvc.services.flex.interfaces.IRemoteObjectService;
	import org.compactmvc.interfaces.IModel;
	import org.compactmvc.patterns.Controller;
	import org.compactmvc.patterns.Model;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.InvokeEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	[Bindable]
	public class RemoteObjectService extends Model implements IModel, IRemoteObjectService
	{
		public static var NAME:String = 'RemoteObjectService';

		private var _remoteObject:RemoteObject;
		
		public function RemoteObjectService(name:String, controller:Controller)
		{
			super(name, controller);
			
				
			//NET ONNECTION
			_remoteObject = new RemoteObject();
			_remoteObject.addEventListener(FaultEvent.FAULT, faultHandler, false, 0, true);
			_remoteObject.addEventListener(ResultEvent.RESULT, resultHandler, false, 0, true);
			_remoteObject.addEventListener(InvokeEvent.INVOKE, invokeHandler, false, 0, true);
		}
		

		override final public function removeHandler():void
		{
			_remoteObject.disconnect();
			_remoteObject.removeEventListener(FaultEvent.FAULT, faultHandler);
			_remoteObject.removeEventListener(ResultEvent.RESULT, resultHandler);
			_remoteObject.removeEventListener(InvokeEvent.INVOKE, invokeHandler);
			_remoteObject = null;
		}
		
		public function faultHandler(fault:FaultEvent):void
		{
			
		}
		
		public function resultHandler(result:ResultEvent):void
		{
			
		}
		
		public function invokeHandler(invoke:InvokeEvent):void
		{
			
		}
		
		/**
		 * returns a given mockup object 
		 * @param mockUp
		 * 
		 */			
		protected final function mockup(mockUp:Object):void
		{ 	
     	 	resultHandler(new ResultEvent(ResultEvent.RESULT,false, false, mockUp));
		}
		
		/**
		 * retruns the remoteObject Instance 
		 * @return 
		 * 
		 */		
		public function get remoteObject():RemoteObject
		{
			return _remoteObject;
		}
		
		/**
		 * @usage: "somyDestination (ServiceName)" 
		 * @param value
		 * 
		 */		
		public final function set destination(value:String):void
		{
			_remoteObject.destination = value;
		}
		
		public final function get destination():String
		{
			return _remoteObject.destination;
		}
		
		/**
		 * @usage: "http://www.myproject.de/myapplication/messagebroker/amf" 
		 * @param value
		 * 
		 */		
		public final function set endpoint(value:String):void
		{
			_remoteObject.endpoint = value;
		}
		
		public final function get endpoint():String
		{
			return _remoteObject.endpoint;
		}
		
		/**
		 * @usage: "my.service.namespace.MyService"; 
		 * @param value
		 * 
		 */		
		public final function set source(value:String):void
		{
			_remoteObject.source = value;
		}
		
		public final function get source():String
		{
			return _remoteObject.source;
		}
	
	}
}